﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DSAClient.Core;
using DSAClient.Models.Input;
using DSAClient.Utils;
using Qiniu.IO.Resumable;
using Qiniu.RS;
using Top.Api.Util;

namespace DSAClient
{
    public partial class Form1 : BaseForm
    {
        public Form1()
        {
            InitializeComponent();

            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.Selectable, true);
            
            //inPutBuffer("");
            srcBitmap = new Bitmap(pictureBox1.Image);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var result = Api.GetTaobaoOrder(new TaobaoOrderGetPara() { no = "111", ww = "222" });
            MessageBox.Show(JsonHelper.SerializeObject(result));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var result = Api.QueryPhotoSize();
            MessageBox.Show(JsonHelper.SerializeObject(result));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var result = Api.GetConfig();
            MessageBox.Show(JsonHelper.SerializeObject(result));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            var result = Api.SaveOrder(new OrderSubmitInfo());
            MessageBox.Show(JsonHelper.SerializeObject(result));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Qiniu.Conf.Config.ACCESS_KEY = "1NVx2CraEl_CzooAqG8qwzjPChBjehBJ_uJjgZnu";
            Qiniu.Conf.Config.SECRET_KEY = "rHIVz90KC-8mM5HhUDn-cNXNzKlrwM2rhiutKMvn";

            string bucketName = "test";
            //PutPolicy put = new PutPolicy(bucketName);
            //string uptoken = put.Token();

            PutPolicy policy = new PutPolicy(bucketName, 3600);
            string upToken = policy.Token();
            Settings setting = new Settings();
            ResumablePutExtra extra = new ResumablePutExtra();
            ResumablePut client = new ResumablePut(setting, extra);
            var result = client.PutFile(upToken, @"D:\用户目录\Pictures\QQ图片20150317133718.jpg", Guid.NewGuid().ToString());
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string url = "https://oauth.taobao.com/authorize";
            Process.Start("iexplore.exe", "https://oauth.taobao.com/authorize?response_type=code&client_id=23096987&redirect_uri=http://www.mzc9.com/api/TopOAuth/Authorize&view=web");
            //Dictionary<string, string> props = new Dictionary<string, string>();
            //props.Add("response_type", "code");
            //props.Add("client_id", "23096987");
            //props.Add("redirect_uri", "http://www.mzc9.com/api/TopOAuth/Authorize");
            //props.Add("view", "web");
            //string s = "";
            //try
            //{
            //    WebUtils webUtils = new WebUtils();
            //    s = webUtils.DoPost(url, props);
            //}
            //catch (IOException ex)
            //{ }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Bitmap bmp = KiRotate90(new Bitmap(pictureBox1.Image));
            pictureBox1.Image = bmp;
        }

        /// <summary>
        /// 顺时针旋转90度
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public static Bitmap KiRotate90(Bitmap img)
        {
            try
            {
                img.RotateFlip(RotateFlipType.Rotate90FlipNone);
                return img;
            }
            catch
            {
                return null;
            }
        }

        /// <summary>
        /// 缩略图
        /// </summary>
        /// <param name="se"></param>
        public void AdjustPhoto(SizeEnum se)
        {

        }

        #region pictureBoxt图片移动
        Point offset = new Point(0, 0);
        Point p;
        Bitmap srcBitmap;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            pictureBox1.Cursor = Cursors.Hand; //按下鼠标时，将鼠标形状改为手型
            //wselected = true;
            p.X = e.X;
            p.Y = e.Y;
        }

        int driftX = 0, driftY = 0;
        int mx = 0, my = 0;
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {

            if (e.Button == MouseButtons.Left)
            {
                offset.Offset(e.X - p.X, e.Y - p.Y);
                this.MyReDrawTest(offset.X, offset.Y);
                p = e.Location;
            }

        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            pictureBox1.Cursor = Cursors.Default; //松开鼠标时，形状恢复为箭头
            //wselected = false;
        }

        private void MyReDrawTest(int x, int y)
        {
            if (srcBitmap == null)
                return;
            BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
            BufferedGraphics myBuffer = currentContext.Allocate(this.pictureBox1.CreateGraphics(), this.pictureBox1.DisplayRectangle);
            myBuffer.Graphics.Clear(this.pictureBox1.BackColor);
            myBuffer.Graphics.DrawImage(srcBitmap, offset);
            myBuffer.Render(this.pictureBox1.CreateGraphics());
            myBuffer.Dispose();
        }
        #endregion

    }
}
